import io
import json
import logging
import os
import webbrowser
import winreg
from threading import Semaphore

import clr

from webview import _settings, settings as webview_settings
from webview.dom import _dnd_state
from webview.js import alert
from webview.js.css import disable_text_select
from webview.util import DEFAULT_HTML, create_cookie, interop_dll_path, js_bridge_call, inject_pywebview

clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Collections')
clr.AddReference('System.Threading')

import System.Windows.Forms as WinForms
from System import Action, Func, String, Type, Uri, Array, Byte
from System.IO import Stream, MemoryStream, StreamReader, SeekOrigin

from System.Collections.Generic import List
from System.Drawing import Color
from System.Globalization import CultureInfo
from System.Threading.Tasks import Task, TaskScheduler
import traceback

clr.AddReference(interop_dll_path('Microsoft.Web.WebView2.Core.dll'))
clr.AddReference(interop_dll_path('Microsoft.Web.WebView2.WinForms.dll'))

from Microsoft.Web.WebView2.Core import CoreWebView2Cookie, CoreWebView2ServerCertificateErrorAction, \
    CoreWebView2Environment
from Microsoft.Web.WebView2.WinForms import CoreWebView2CreationProperties, WebView2
from Microsoft.Web.WebView2.Core import CoreWebView2WebResourceContext

for platform in ('win-arm64', 'win-x64', 'win-x86'):
    os.environ['Path'] += ';' + interop_dll_path(platform)

logger = logging.getLogger('pywebview')


class EdgeChrome:
    def __init__(self, form, window, cache_dir):
        self.pywebview_window = window
        self.web_view = WebView2()
        props = CoreWebView2CreationProperties()
        props.UserDataFolder = cache_dir
        props.set_IsInPrivateModeEnabled(_settings['private_mode'])
        props.AdditionalBrowserArguments = '--disable-features=ElasticOverscroll'

        if webview_settings['ALLOW_FILE_URLS']:
            props.AdditionalBrowserArguments += ' --allow-file-access-from-files'

        self.web_view.CreationProperties = props

        self.form = form
        form.Controls.Add(self.web_view)

        self.js_results = {}
        self.js_result_semaphore = Semaphore(0)
        self.web_view.Dock = WinForms.DockStyle.Fill
        self.web_view.BringToFront()
        self.web_view.CoreWebView2InitializationCompleted += self.on_webview_ready
        self.web_view.NavigationStarting += self.on_navigation_start
        self.web_view.NavigationCompleted += self.on_navigation_completed
        self.web_view.WebMessageReceived += self.on_script_notify
        self.syncContextTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext()
        self.web_view.DefaultBackgroundColor = Color.FromArgb(255, int(window.background_color.lstrip("#")[0:2], 16),
                                                              int(window.background_color.lstrip("#")[2:4], 16),
                                                              int(window.background_color.lstrip("#")[4:6], 16))

        if window.transparent:
            self.web_view.DefaultBackgroundColor = Color.Transparent

        self.url = None
        self.ishtml = False
        self.html = DEFAULT_HTML
        self.setup_webview2_environment()

    def setup_webview2_environment(self):
        def _callback(task):
            self.web_view.EnsureCoreWebView2Async(task.Result)

        environment = CoreWebView2Environment.CreateAsync()
        environment.ContinueWith(
            Action[Task[CoreWebView2Environment]](_callback),
            self.syncContextTaskScheduler,
        )

    def evaluate_js(self, script, semaphore, js_result, callback=None):
        def _callback(result):
            if callback is None:
                result = None if result is None or result == '' else json.loads(result)
                js_result.append(result)
                semaphore.release()
            else:
                # future js callback option to handle async js method
                callback(result)
                js_result.append(None)
                semaphore.release()

        try:
            self.web_view.ExecuteScriptAsync(script).ContinueWith(
                Action[Task[String]](lambda task: _callback(json.loads(task.Result))),
                self.syncContextTaskScheduler,
            )
        except Exception:
            logger.exception('Error occurred in script')
            js_result.append(None)
            semaphore.release()

    def clear_cookies(self):
        self.web_view.CoreWebView2.CookieManager.DeleteAllCookies()

    def get_cookies(self, cookies, semaphore):
        def _callback(task):
            for c in task.Result:
                _cookies.append(c)

            self.web_view.Invoke(Func[Type](_parse_cookies))

        def _parse_cookies():
            # cookies must be accessed in the main thread, otherwise an exception is thrown
            # https://github.com/MicrosoftEdge/WebView2Feedback/issues/1976
            for c in _cookies:
                same_site = None if c.SameSite == 0 else str(c.SameSite).lower()
                try:
                    data = {
                        'name': c.Name,
                        'value': c.Value,
                        'path': c.Path,
                        'domain': c.Domain,
                        'expires': c.Expires.ToString('r', CultureInfo.GetCultureInfo('en-US')),
                        'secure': c.IsSecure,
                        'httponly': c.IsHttpOnly,
                        'samesite': same_site,
                    }

                    cookie = create_cookie(data)
                    cookies.append(cookie)
                except Exception as e:
                    logger.exception(e)

            semaphore.release()

        _cookies = []
        self.web_view.CoreWebView2.CookieManager.GetCookiesAsync(self.url).ContinueWith(
            Action[Task[List[CoreWebView2Cookie]]](_callback), self.syncContextTaskScheduler
        )

    def get_current_url(self):
        return self.url

    def load_html(self, content, _):
        self.html = content
        self.ishtml = True
        self.pywebview_window.events.loaded.clear()

        if self.web_view.CoreWebView2:
            self.web_view.CoreWebView2.NavigateToString(self.html)
        else:
            self.web_view.EnsureCoreWebView2Async(None)

    def load_url(self, url):
        self.ishtml = False
        self.web_view.Source = Uri(url)

    def on_certificate_error(self, _, args):
        args.set_Action(CoreWebView2ServerCertificateErrorAction.AlwaysAllow)

    def on_script_notify(self, _, args):
        try:
            return_value = args.get_WebMessageAsJson()

            if return_value == '"FilesDropped"':
                if _dnd_state['num_listeners'] == 0:
                    return
                additionalObjects = args.get_AdditionalObjects()
                if additionalObjects is None:
                    return

                files = [
                    (os.path.basename(file.Path), file.Path)
                    for file
                    in list(additionalObjects)
                    if 'CoreWebView2File' in str(type(file))
                ]
                _dnd_state['paths'] += files
                return

            func_name, func_param, value_id = json.loads(return_value)
            func_param = json.loads(func_param)
            if func_name == '_pywebviewAlert':
                WinForms.MessageBox.Show(func_param)
            elif func_name == 'console':
                print(func_param)
            else:
                js_bridge_call(self.pywebview_window, func_name, func_param, value_id)
        except Exception:
            logger.exception('Exception occurred during on_script_notify')

    def on_new_window_request(self, sender, args):
        args.set_Handled(True)

        if webview_settings['OPEN_EXTERNAL_LINKS_IN_BROWSER']:
            webbrowser.open(str(args.get_Uri()))
        else:
            self.load_url(str(args.get_Uri()))

    def on_web_resource_requested(self, sender, args):
        # 使用Python requests发送请求，然后组装response

        def read_irandom_access_stream_to_bytes(stream):
            if stream is None:
                return None
            # print('read_irandom_access_stream_to_bytesess_stream_to_bytes', stream, stream.ToString())
            stream.Seek(0, SeekOrigin.Begin)  # 确保从流的开头读取
            size = stream.Length
            buffer = Array.CreateInstance(Byte, size)
            stream.Read(buffer, 0, buffer.Length)
            return bytearray(buffer)

        try:
            request = args.Request
            headers = request.Headers
            py_headers = {}
            for header in headers:
                py_headers[header.Key] = header.Value
            data_stream = read_irandom_access_stream_to_bytes(request.Content)
            # data = data_stream.decode('utf-8')
            py_args = {
                'request': {
                    'uri': request.Uri,
                    'method': request.Method,
                    'headers': py_headers,
                    'data_stream': data_stream,
                },
                'response': {}
            }
            should_update = self.pywebview_window.events.request_sent.set(args=py_args)
            if not should_update:
                return

            py_request = py_args.get('request', {})
            if py_request and len(py_request) > 0:
                py_uri = py_request.get('uri')
                uri = Uri(py_uri)
                py_data_stream = py_request.get('data_stream')
                request_memory_stream = MemoryStream(py_data_stream) if py_data_stream is not None else None

                request_headers = py_request.get('headers',{})
                args.Request.Uri = py_uri
                if request_memory_stream is not None:
                    args.Request.Content = request_memory_stream

                for header in args.Request.Headers:
                    if header.Key in request_headers:
                        args.Request.Headers.RemoveHeader(header.Key)
                for k,v in request_headers.items():
                    if v:
                        args.Request.Headers.SetHeader(k, v)
                    else:
                        args.Request.Headers.RemoveHeader(k)

            # 如果返回有response，则直接返回请求
            py_response: dict = py_args.get('response', {})
            if py_response is not None or py_response.get('status_code') is not None:
                return
            res_headers = [f'{key}:{value}' for key, value in py_response.get('headers')]
            bytes_io = io.BytesIO(py_response.get('content'))
            byte_array = Array[Byte](bytes_io.getvalue())
            memory_stream = MemoryStream(byte_array)
            response = self.web_view.CoreWebView2.Environment.CreateWebResourceResponse(
                memory_stream, py_response.get('status_code', 200), py_response.get('reason_phrase', 'OK'),
                '\n'.join(res_headers));
            args.Response = response
        except Exception as e:
            print('on_web_resource_response', e)
            traceback.print_exc()

    def on_web_response_received(self, sender, args):
        # print('on_web_response_received')
        # 使用Python requests发送请求，然后组装response

        def read_irandom_access_stream_to_bytes(stream):
            if stream is None:
                return None
            # print('read_irandom_access_stream_to_bytesess_stream_to_bytes', stream, stream.ToString())
            stream.Seek(0, SeekOrigin.Begin)  # 确保从流的开头读取
            size = stream.Length
            buffer = Array.CreateInstance(Byte, size)
            stream.Read(buffer, 0, buffer.Length)
            return bytearray(buffer)

        def _callback(task):
            # print('on_web_response_received callback',task)
            content = task.Result
            try:
                stream = read_irandom_access_stream_to_bytes(content)
                request = args.Request
                request_headers = {}
                for header in request.Headers:
                    request_headers[header.Key] = header.Value
                response_headers = {}
                for header in args.Response.Headers:
                    response_headers[header.Key] = header.Value
                request_stream = read_irandom_access_stream_to_bytes(request.Content)

                py_args = {
                    'request': {
                        'uri': request.Uri,
                        'method': request.Method,
                        'headers': request_headers,
                        'data_stream': request_stream,
                    },
                    'response': {
                        'headers': response_headers,
                        'status_code': args.Response.StatusCode,
                        'reason_phrase': args.Response.ReasonPhrase,
                        'content': stream,
                    }
                }

                self.pywebview_window.events.response_received.set(args=py_args)
            except Exception as e:
                traceback.print_exc()
            pass

        try:
            # data = data_stream.decode('utf-8')
            # print('on_web_response_received GetContentAsync',args.Response.GetContentAsync)
            content = args.Response.GetContentAsync().ContinueWith(
                Action[Task[Stream]](_callback), self.syncContextTaskScheduler
            )
            return
            py_args = {
                'request': {
                    'uri': request.Uri,
                    'method': request.Method,
                    'headers': py_headers,
                    'data_stream': data_stream,
                },
                'response': {}
            }
            # print(py_args)
            py_response: dict = py_args.get('response', {})
            # print('on_web_resource_response',py_response, py_args)
            if py_response is not None or py_response.get('status_code') is not None:
                return
            res_headers = [{f'{key}:{value}'} for key, value in py_response.get('headers')]
            bytes_io = io.BytesIO(py_response.get('content'))
            byte_array = Array[Byte](bytes_io.getvalue())
            memory_stream = MemoryStream(byte_array)
            response = self.web_view.CoreWebView2.Environment.CreateWebResourceResponse(
                memory_stream, py_response.get('status_code', 200), py_response.get('reason_phrase', 'OK'),
                '\n'.join(res_headers));
            args.Response = response
        except Exception as e:
            print('on_web_resource_response', e)
            traceback.print_exc()

    def on_source_changed(self, sender, args):
        self.url = sender.Source
        self.ishtml = False

    def on_webview_ready(self, sender, args):
        if not args.IsSuccess:
            logger.error(
                'WebView2 initialization failed with exception:\n '
                + str(args.InitializationException)
            )
            return

        # print(self.web_view,self.web_view.CoreWebView2.AddWebResourceRequestedFilter)
        self.web_view.CoreWebView2.AddWebResourceRequestedFilter('*', CoreWebView2WebResourceContext.All)
        self.web_view.CoreWebView2.WebResourceRequested += self.on_web_resource_requested
        self.web_view.CoreWebView2.WebResourceResponseReceived += self.on_web_response_received
        self.web_view.CoreWebView2.SourceChanged += self.on_source_changed
        sender.CoreWebView2.NewWindowRequested += self.on_new_window_request

        if _settings['ssl']:
            sender.CoreWebView2.ServerCertificateErrorDetected += self.on_certificate_error

        sender.CoreWebView2.DownloadStarting += self.on_download_starting

        settings = sender.CoreWebView2.Settings
        settings.AreBrowserAcceleratorKeysEnabled = _settings['debug']
        settings.AreDefaultContextMenusEnabled = _settings['debug']
        settings.AreDefaultScriptDialogsEnabled = True
        settings.AreDevToolsEnabled = _settings['debug']
        settings.IsBuiltInErrorPageEnabled = True
        settings.IsScriptEnabled = True
        settings.IsWebMessageEnabled = True
        settings.IsStatusBarEnabled = _settings['debug']
        settings.IsSwipeNavigationEnabled = False
        settings.IsZoomControlEnabled = True

        if _settings['user_agent']:
            settings.UserAgent = _settings['user_agent']

        if _settings['private_mode']:
            # cookies persist even if UserDataFolder is in memory. We have to delete cookies manually.
            sender.CoreWebView2.CookieManager.DeleteAllCookies()

        if self.pywebview_window.real_url:
            self.load_url(self.pywebview_window.real_url)
        elif self.pywebview_window.html:
            self.html = self.pywebview_window.html
            self.load_html(self.pywebview_window.html, '')
        else:
            self.load_html(DEFAULT_HTML, '')

        if _settings['debug'] and webview_settings['OPEN_DEVTOOLS_IN_DEBUG']:
            sender.CoreWebView2.OpenDevToolsWindow()

    def on_download_starting(self, sender, args):
        if not webview_settings['ALLOW_DOWNLOADS']:
            args.Cancel = True
            return

        dialog = WinForms.SaveFileDialog()

        try:
            with winreg.OpenKey(winreg.HKEY_CURRENT_USER,
                                r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders') as windows_key:
                dialog.InitialDirectory = winreg.QueryValueEx(windows_key, '{374DE290-123F-4565-9164-39C4925E467B}')[0]
        except Exception as e:
            logger.exception(e)

        dialog.Filter = self.pywebview_window.localization['windows.fileFilter.allFiles'] + ' (*.*)|*.*'
        dialog.RestoreDirectory = True
        dialog.FileName = os.path.basename(args.ResultFilePath)

        result = dialog.ShowDialog(self.form)
        if result == WinForms.DialogResult.OK:
            args.ResultFilePath = dialog.FileName
        else:
            args.Cancel = True

    def on_navigation_start(self, sender, args):
        pass

    def on_navigation_completed(self, sender, _):
        url = str(sender.Source)
        self.url = None if self.ishtml else url

        self.web_view.ExecuteScriptAsync(inject_pywebview(self.pywebview_window, 'chromium'))
        self.web_view.ExecuteScriptAsync(alert.src % {'platform': 'edgechromium'})

        if not self.pywebview_window.text_select:
            self.web_view.ExecuteScriptAsync(disable_text_select)

        self.pywebview_window.events.loaded.set()
